/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.power.powercash;

import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;
import com.power.utils.Date;
import java.util.List;

/**
 *
 * @author f6036477
 */
public class Saldo
{

  private Banco banco;

  private Date date, before;

  private ObjectContainer db;

  private double saldo;


  public Saldo(ObjectContainer db)
  {
    this.db = db;
    saldo = 0.0;
    banco = null;
    date = null;
    before = null;
  }

  public Saldo(ObjectContainer db, Banco b, Date d)
  {
    this.db = db;
    saldo = 0.0;
    banco = b;
    date = d;
  }


  public Saldo(ObjectContainer db, Banco b, Date d, Date before)
  {
    this.db = db;
    saldo = 0.0;
    banco = b;
    date = d;
    this.before = before;
  }


  public Banco getBanco()
  {
    return banco;
  }


  public double getTotal()
  {
    return saldo;
  }


  public void clearTotal()
  {
    saldo = 0;
  }


  public void setBanco(Banco b)
  {
    banco = b;
  }


  public Date getDate()
  {
    return date;
  }


  public void setDate(Date d)
  {
    date = d;
  }


  public Date getDateBefore()
  {
    return before;
  }


  public void setDateBefore(Date d)
  {
    before = d;
  }


  public void calc()
  {
    Lancamento l = new Lancamento();
    l.setDataBanco(date);

    if(banco != null) {
      Pagamento p = new AbstractPagamento() {};
      p.setBanco(banco);
      l.setPagamento(p);
    }//if

    ConsultaLancamento cl = new ConsultaLancamento(l);
    if(before != null) {
      cl.getConsultaDate().setDateBefore(before);
      cl.getConsultaDate().setCompareDateMethod(CompareDate.COMPARE_BETWEEN);
    }//if
    else
      cl.getConsultaDate().setCompareDateMethod(CompareDate.COMPARE_BEFORE);

    List<Lancamento> list = db.query(cl);
    for(Lancamento lc : list)
      if(lc.isDebito())
        saldo -= lc.getPagamento().getValor();
      else
        saldo += lc.getPagamento().getValor();
  }
  
  
  @Override
  public String toString()
  {
    return "[Saldo] : "+ Formatter.format(Moeda.REAL, 1, saldo, 2)+
        (banco != null ? " : " + banco.toString() : "");
  }


  public static void main(String[] args)
  {
    Configuration conf = new Configuration();
    conf.setDBFile("powercash.db");
    ObjectContainer db = conf.startDB();

    Lancamento l;

    Banco b = new Banco();
    b.setName("BB");
    b.setAgencia(1);
    b.setConta(1);

    Date d = new Date();
    d.sub(Date.MES, 1);

    Dinheiro pag = new Dinheiro();
    pag.setBanco(b);
    pag.setValor(50);

    System.out.println("add: 10 x 50 : " + d.getDateStringFormat());
    for(int i = 0; i < 10; i++) {
      l = new Lancamento();
      l.setDataBanco(d);
      l.setPagamento(pag.clone());
      db.store(l);
    }

    d = new Date();
    b = new Banco("Bradesco", 2, 2);
    pag.setBanco(b);
    System.out.println("add: 10 x 50 : " + d.getDateStringFormat());
    for(int i = 0; i < 10; i++) {
      l = new Lancamento();
      l.setDataBanco(d);
      l.setPagamento(pag.clone());
      db.store(l);
    }

    b = new Banco("BB", 1, 1);

    d = Date.parseDate("01.08.2010");
    Saldo s = new Saldo(db, null, d);

    System.out.println("Search before : " + d.getDateStringFormat());
    System.out.println("s.calc()");
    s.calc();
    System.out.println("saldo: "+ s.toString());

    b = new Banco("Bradesco", 2, 2);
    d = new Date();
    d.add(Date.DIA, 1);
    s.setDate(d);

    System.out.println("s.clearTotal()");
    s.clearTotal();
    System.out.println("Search before : " + d.getDateStringFormat());
    System.out.println("s.calc()");
    s.calc();
    System.out.println("saldo: "+ s.toString());
  }

}
